package cn.hpclub.server.validator.admin;

import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.validate.Validator;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.controller.admin.PackageController;

public class PackageValidator extends Validator{

    private static final String PACKAGE_NAME_MESSAGES = "packageNameMessages";
    private static final String CODE_MESSAGES         = "codeMessages";
    private static final String CHARGE_MESSAGES       = "chargeMessages";
    private static final String PREDEPOSITED_MESSAGES = "predepositedMessages";

    @Override
    protected void validate(Controller c){
        validateRequiredString(PackageController.NAME, PACKAGE_NAME_MESSAGES, "套餐名称不能为空");
        this.setShortCircuit(true);
        validateRequiredString(PackageController.CODE, CODE_MESSAGES, "资费代码不能为空");
        validateRegex(PackageController.CODE, Constant.Regex.ALPHABETS_NUMBERS, CODE_MESSAGES, "资费代码只能是数字或字母");

        // if(!Package.dao.checkUnique(c.getParaToInt(Package.ID),
        // Package.TARIFF_CODE,
        // c.getPara(PackageController.CODE))){
        // addError(CODE_MESSAGES, "资费代码已存在");
        //
        // }
        this.setShortCircuit(false);

        this.setShortCircuit(true);

        validateRequiredString(PackageController.CHARGE, CHARGE_MESSAGES, "资费不能为空");
        String charge = c.getPara(PackageController.CHARGE);
        if(StrKit.notBlank(charge)){
            validateRegex(PackageController.CHARGE, Constant.Regex.DOUBLE_PRICE, CHARGE_MESSAGES, "资费应为最多两位小数价格数字");
            if(Double.valueOf(charge).compareTo(Double.valueOf("9999999999.99")) > 0){
                addError(CHARGE_MESSAGES, "资费不能超过9999999999.99");
            }
        }
        this.setShortCircuit(false);

        this.setShortCircuit(true);
        /*validateRequiredString(PackageController.PREDEPOSITED, PREDEPOSITED_MESSAGES, "预存资金不能为空");
        String predeposited = c.getPara(PackageController.PREDEPOSITED);
        if(StrKit.notBlank(c.getPara(PackageController.PREDEPOSITED))){
            validateRegex(PackageController.PREDEPOSITED, Constant.Regex.DOUBLE_PRICE, PREDEPOSITED_MESSAGES,
                          "预存资金应为最多两位小数价格数字");
            if(Double.valueOf(predeposited).compareTo(Double.valueOf("9999999999.99")) > 0){
                addError(PREDEPOSITED_MESSAGES, "预存资金不能超过9999999999.99");
            }
        }
        this.setShortCircuit(false);*/
    }

    @Override
    protected void handleError(Controller c){
        c.keepPara();
        c.render(PackageController.HTML_ADD);
    }
}
